package com.jivam.lottery.model;

import java.io.Serializable;
/**
 * 每个具体的小公式的右边项，包含统计功能
 * @author Jivam
 */
public class Formula implements Serializable{

	private static final long serialVersionUID = 8309732216962932621L;
	private Expression expression;//公式
	
	private int total;//导入期数
	private int right;//正确期数
	
	boolean lastRight;//上一次是否正确
	private int doubleRight;//连续2次正确几率

	public Formula(String expression) {
		super();
		this.expression = new Expression(expression);
	}

	public String getExpression() {
		return expression.getExpression();
	}

	public String getPostfixexp() {
		return expression.getPostfixexp();
	}

	public void addValue(boolean thisRight) {
		total = total + 1;
		if(thisRight){
			right = right + 1;
			if(lastRight)
				doubleRight = doubleRight + 1;
			lastRight = true;
		}else{
			lastRight = false;
		}
	}
	
	/**
	 * 获得正确率
	 * @return
	 */
	public double getRightRate(){
		return getRate(right, total);
	}

	public double getWrongRate(){
		return getRate(total-right, total);
	}
	
	/**
	 * 连续2期正确率
	 * @return
	 */
	public double getDoubleRightRate(){
		return getRate(doubleRight, total-1);
	}
	
	public double getDoubleWrongRate(){
		return getRate(total-doubleRight-1, total-1);
	}
	
	private static final double HUNDRED = 100;
	private double getRate(int right,int total){
		double a = right;
		double b = total;
		return Math.round(a/b*HUNDRED * HUNDRED)/HUNDRED;
	}
	

	@Override
	public String toString() {
		return expression.getExpression();
	}
	
}
